package com.zr.useredu.controller;

import com.zr.meiju.XueliEnum;
import com.zr.meiju.ZhiyeEnum;
import com.zr.useredu.pojo.UserEduAddVo;
import com.zr.useredu.pojo.XiaLaVo;
import com.zr.useredu.service.UserEduService;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 修改教育接口
 * Created by star on 2019/7/3.
 */
@RestController
public class UserEduController {
    @Autowired
    private UserEduService userEduService;

    /**
     * 根据Loginid进行查询
     */
    @GetMapping("queryLoginIdEdu")
    public ResultVo<UserEduAddVo> queryLoginIdEdu(HttpServletRequest request){
        Integer loginId = (Integer)request.getSession().getAttribute("loginId");
        System.out.println(loginId);
        return userEduService.queryLoginIdEdu(loginId);
    }

    /**
     * 职业的下拉框
     */
    @GetMapping("zhiyeDropdownBox")
    public ResultVo<List<XiaLaVo>> zhiyeDropdownBox(){

        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< ZhiyeEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setId(i);
            xiaLaVo.setName(ZhiyeEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }

    /**
     * 学历的下拉框
     */
    @GetMapping("xueliDropdownBox")
    public ResultVo<List<XiaLaVo>> xueliDropdownBox(){

        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< XueliEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setId(i);
            xiaLaVo.setName(XueliEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }



    /**
     * 添加教育接口
     */
    @PostMapping("/addEducation")
    public ResultVo addEducation(@RequestBody @Valid UserEduAddVo userEduAddVo, BindingResult bindingResult,HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(), "500");
        }
        return userEduService.addEducation(userEduAddVo,request);
    }
}
